package com.example.demo.service.impl;

import com.example.demo.entity.*;
import com.example.demo.mapper.*;
import com.example.demo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private SchoolMapper schoolMapper;

    @Autowired
    private MajorMapper majorMapper;

    @Autowired
    private CourseRelationMapper courseRelationMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public HashMap<String,String> roomAvailable(boolean roomExists, String ctime, String croom){
        HashMap<String, String> hashMap = new HashMap<>();
        if (roomExists) {
            String[] time = ctime.split(",");
            List<Integer> timesInt = new ArrayList<>();
            for (String s : time) {
                int timeInt = Integer.parseInt(s);
                timesInt.add(timeInt);
            }
            if (courseRelationMapper.findByRoomAndPeriods(timesInt, croom) > 0) {
                //isValid = false;
                hashMap.put("room", "该时段该地点不空闲");
            } else {
                hashMap.put("room", "ok");
            }
        } else {
            hashMap.put("room", "教室不存在");
        }
        return hashMap;
    }

    @Override
    public HashMap<String,String> teacherAvailable(String ctime, User teacher){
        HashMap<String,String> hashMap = new HashMap<>();
        String[] time = ctime.split(",");
        List<Integer> timesInt = new ArrayList<>();
        for (String s : time) {
            int timeInt = Integer.parseInt(s);
            timesInt.add(timeInt);
        }
        if (courseRelationMapper.findByTeacherAndPeriod(timesInt, teacher.getID()) > 0) {
            //isValid = false;
            hashMap.put("teacher", "该时段该教师不空闲");
        } else {
            hashMap.put("teacher", "ok");
        }
        return hashMap;
    }

    @Override
    public HashMap<String,String> schoolMajorExist(List<String> snameList, List<String> mnameList,String csname,String cmname){
        HashMap<String,String> hashMap = new HashMap<>();
        if (!snameList.contains(csname)) {
            System.out.println("开课学院不存在");
            hashMap.put("school", "开课学院不存在");
            //isValid = false;
        } else {
            hashMap.put("school", "ok");
        }
        if (!mnameList.contains(cmname)) {
            System.out.println("开课专业不存在");
            hashMap.put("major", "开课专业不存在");
            //isValid = false;
        } else {
            hashMap.put("major", "ok");
        }
        return hashMap;
    }

    @Override
    public HashMap<String,String> capacityOk(boolean roomExists, Room room1, Integer capacity){
        HashMap<String,String> hashMap = new HashMap<>();
        if (roomExists) {
            if (room1.getCapacity() < capacity) {
                System.out.println("容量超过教室限制");
                hashMap.put("capacity", "容量超过教室限制");
                //isValid = false;
            } else {
                hashMap.put("capacity", "ok");
            }
        } else {
            hashMap.put("capacity", "教室不存在");
        }
        return hashMap;
    }

    @Override
    public HashMap<String, String> validateCourse(Course course, User teacher) {
        HashMap<String, String> hashMap = new HashMap<>();

        if (course == null) {
            hashMap.put("result", "请求失败");
            return hashMap;
        }

        String ctime = course.getCtime();
        String croom = course.getCroom();
        String csname = course.getCsname();
        String cmname = course.getCmname();
        Integer capacity = course.getCapacity();
        Room room1 = roomMapper.getById(croom);

        boolean isValid = true;
        boolean roomExists = false;

        List<String> snameList = schoolMapper.selectNameAll();
        List<String> mnameList = majorMapper.selectNameAll();

        if (roomMapper.existsById(croom)) {
            hashMap.put("room", "ok");
            roomExists = true;
        } else {
            isValid = false;
            hashMap.put("room", "教室不存在");
        }

        HashMap<String,String> roomFreeMap = roomAvailable(roomExists,ctime,croom);
        if(!Objects.equals(roomFreeMap.get("room"), "ok"))
            isValid=false;
        hashMap.putAll(roomFreeMap);

        HashMap<String,String> teacherFreeMap = teacherAvailable(ctime,teacher);
        if(!Objects.equals(teacherFreeMap.get("teacher"), "ok"))
            isValid=false;
        hashMap.putAll(teacherFreeMap);

        HashMap<String,String> schoolMajorMap = schoolMajorExist(snameList,mnameList,csname,cmname);
        if(!Objects.equals(schoolMajorMap.get("school"), "ok"))
            isValid=false;
        if(!Objects.equals(schoolMajorMap.get("major"), "ok"))
            isValid=false;
        hashMap.putAll(schoolMajorMap);

        HashMap<String,String> capacityMap = capacityOk(roomExists,room1,capacity);
        if(!Objects.equals(capacityMap.get("capacity"), "ok"))
            isValid=false;
        hashMap.putAll(capacityMap);


        if (isValid) {
            course.setCstatus("toAdd");
            hashMap.put("result", "success");
        } else {
            hashMap.put("result", "提交失败");
        }
        return hashMap;
    }


    @Override
    public boolean validateCourseNoCheckSame(HashMap<String, String> hashMap,Course cCourse,Course course) {
        List<String> key_error = new ArrayList<>();
        for (HashMap.Entry<String,String> entry:hashMap.entrySet()){
            if (!entry.getValue().equals("ok")){
                //查找value不等于ok或success所对应的key值
                key_error.add(entry.getKey());
            }
        }
        boolean isValid = true;
        System.out.println(key_error);
        for (String s : key_error) {
            if (Objects.equals(s, "room") || Objects.equals(s, "teacher") || Objects.equals(s, "result")) {

            }
            else
                isValid = false;
        }
        if(isValid){
            if(!Objects.equals(cCourse.getCroom(), course.getCroom())){
                isValid=false;
                System.out.println(cCourse.getCroom());
                System.out.println(course.getCroom());

            }
            if(!Objects.equals(cCourse.getCteacher(), course.getCteacher())){
                System.out.println(cCourse.getCteacher());
                System.out.println(course.getCteacher());
                isValid=false;

            }
        }
        return isValid;
    }

    @Override
    public void copyCourse(Course cCourse, Course course,User teacher){
        //修改
        course.setCnum(cCourse.getCnum());
        course.setCname(cCourse.getCname());
        course.setCsname(cCourse.getCsname());
        course.setChour(cCourse.getChour());
        course.setCcredit(cCourse.getCcredit());
        course.setCteacher(cCourse.getCteacher());
        course.setCintro(cCourse.getCintro());
        course.setCtime(cCourse.getCtime());
        course.setCroom(cCourse.getCroom());
        course.setCapacity(cCourse.getCapacity());
        course.setCstatus(cCourse.getCstatus());
        course.setCmname(cCourse.getCmname());
        //修改
        School newSchool = schoolMapper.findSchoolByName(cCourse.getCsname());
        course.setSchool(newSchool);
        Major newMajor = majorMapper.findMajorByName(cCourse.getCmname());
        course.setMajor(newMajor);
        course.setTeacher(teacher);

        //保存
        courseMapper.save(course);
    }

}


